package com.qf.ayc.foster.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.ayc.foster.entity.Foster;
import com.qf.ayc.foster.service.IFosterService;
import com.qf.ayc.foster.vo.FosterVo;
import com.qf.ayc.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;



/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-02-20
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/foster")
public class FosterController {
    @Autowired
    private IFosterService iFosterService;

    //-----------------------------------------------------------------------------------
    //查询一条寄养信息
//    @PostMapping("/find")
//    public ResultVo find(@RequestBody Foster foster) {
//        List<Foster> fosterlist = iFosterService.find(foster);
//        if (fosterlist.size() == 0) {
//            return ResultVo.ok("-1", "没查到相关信息", fosterlist);
//        }
//        return ResultVo.ok("1", "查询寄养信息成功", fosterlist);
//    }

    //-----------------------------------------------------------------------------------
    //删除一条寄养信息
    //localhost:8083/foster/del?id=6
    @GetMapping("/del")
    public ResultVo del(String id) {
        Boolean flag = iFosterService.del(id);
        if (flag) {
            return ResultVo.ok("1", "删除寄养信息成功");
        } else {
            return ResultVo.ok("-1", "删除寄养信息失败");
        }
    }

    //-----------------------------------------------------------------------------------
    //修改一条寄养信息
    //localhost:8083/foster/update
    //  {
    //            "id": 10,
    //            "username": "15666666666",
    //            "petname": "大橘乖乖",
    //            "type": "猫",
    //            "starttime": "2024-02-26T11:38:20.000+0000",
    //            "endtime": "2024-02-27T11:38:34.000+0000",
    //            "status": 1,
    //            "phone": "15666666666"
    //        }
    @PostMapping("/update")
    public ResultVo updatefoster(@RequestBody Foster foster) {
        log.info("controller层   foster:{}",foster);
        Boolean flag = iFosterService.updatefoster(foster);
        if (flag) {
            return ResultVo.ok("1", "修改寄养信息成功");
        } else {
            return ResultVo.ok("-1", "修改寄养信息失败");
        }
    }

    //-----------------------------------------------------------------------------------
    //返回所有寄养信息
    //localhost:8083/foster/fosterlist
    @PostMapping("/fosterlist")
    public ResultVo getlist(@RequestBody Foster foster,Integer pageNum, Integer limit) {

        log.info("对象{},第几页{},一页多少条{}",foster,pageNum,limit);
        FosterVo alllist = iFosterService.getlist(foster,pageNum, limit);
        return ResultVo.ok("1", "所有寄养信息分页", alllist);
    }

    //-----------------------------------------------------------------------------------
    //添加寄养信息
    //localhost:8083/foster/add
    //  {
    //            "id": 10,
    //            "username": "15666666666",
    //            "petname": "大橘",
    //            "type": "猫",
    //            "starttime": "2024-02-26T11:38:20.000+0000",
    //            "endtime": "2024-02-27T11:38:34.000+0000",
    //            "status": 1,
    //            "phone": "15666666666"
    //        }
    @PostMapping("/add")
    public ResultVo add(@RequestBody Foster foster) {
        Boolean flag = iFosterService.add(foster);
        if (flag) {
            return ResultVo.ok("1", "添加寄养信息成功");
        } else {
            return ResultVo.ok("-1", "添加寄养信息失败");
        }
    }

    @GetMapping("/total")
    public ResultVo total() {
        long count = iFosterService.count();
        System.out.println(count + "寄养信息总条目数为");
        return ResultVo.ok("1", "寄养信息总条目数", count);
    }
    @GetMapping("/statustotal")
    public ResultVo statustotal(Integer status){
        QueryWrapper<Foster> wrapper = new QueryWrapper<>();
        wrapper.eq("status",status);
        long count = iFosterService.count(wrapper);
        System.out.println(count + "寄养信息寄养情况总条目数为");
        return ResultVo.ok("1", "寄养信息寄养情况总条目数为", count);
    }
}
